import time

def bubble_sort(list1):
    n = len(list1)
    move = True
    start_time = time.time()

    while move:
        move = False
        for i in range (1, n):
            if list1[i-1] > list1[i]:
                list1[i - 1], list1[i] = list1[i], list1[i-1]
                move = True
    end_time = time.time()
    spend_time = int((end_time - start_time)*1000)
    return spend_time, list1


def save_file(i, sort_list):
    with open(i, "w") as files:
        for value in sort_list:
            files.write(f"{value}\n")


def main():
    files = ["m1_sorted.txt", "m2_reverse.txt", "m3_shuffle.txt"]
    for i in files:
        with open(i, "r") as files:
            numbers = [int(line.strip()) for line in files]
        spend_time, sort_list = bubble_sort(numbers)
        print(i + ":", spend_time, "milliseconds")
        save_file(i, sort_list)
if __name__ == "__main__":
    main()